Hand Gesture Controlled Servo Using Python & Arduino
Date: فبراير 20, 2025
Value: FREE

Hand Gesture Controlled Servo Using Python & Arduino 🤖

Simple Explanation:

This project uses hand tracking to control a servo motor. When you move your fingers, the servo angle changes based on the distance between your thumb and index finger

🛠️ Requirements

🔹 Required Components

Component

Description

Arduino Uno

The microcontroller board for processing data.

Servo Motor

Rotates based on hand gestures.

Computer

Runs the Python script.

USB Cable

Connects Arduino to the computer.

🔹 Required Libraries

Library

Installation Command

Purpose

OpenCV

pip install opencv-python

Used for image processing.

MediaPipe

pip install mediapipe

Detects hand gestures.

PySerial

pip install pyserial

Enables communication between Python & Arduino.

🔹 Required Software

SoftwareInstallation LinkPurpose
Python (pip)Download PythonRuns the hand tracking script.
Arduino IDEDownload Arduino IDEUploads code to the Arduino board.
VS CodeDownload VS CodeCode editor for Python and Arduino.

📌 Steps to Follow:

🖥️ 1- Setting Up Python Environment:

make sure that you install the Python program and activate the pip function.

Then Open CMD in your PC and copy this comand

				
					pip install opencv-python mediapipe pyserial
				
			

🔌 2- Connecting the Components:

🔧 3- Uploading the Arduino Code:

Make sure you select the right port then upload this code to your Arduino using Arduino IDE:

				
					#include <Servo.h>

Servo myServo;
int angle;

void setup() {
  myServo.attach(9);  // Servo connected to pin 9
  Serial.begin(9600); // Begin serial communication
}

void loop() {
  if (Serial.available() > 0) {
    angle = Serial.parseInt();  // Read the angle from Python
    if (angle >= 0 && angle <= 180) {
      myServo.write(angle);     // Set the servo angle
    }
  }
}

				
			

2️⃣ Run the Python Code Below:

 please check the Python program you selected same port as the Arduino.
				
					import cv2
import mediapipe as mp
import serial
import time
import math

# Set up serial communication (update the COM port as per your system)
arduino = serial.Serial('COM9', 9600)  # Replace 'COM4' with your Arduino's COM port
time.sleep(2)  # Wait for Arduino to initialize

# Initialize MediaPipe Hand Detection
mp_hands = mp.solutions.hands
hands = mp_hands.Hands(max_num_hands=1, min_detection_confidence=0.7)
mp_draw = mp.solutions.drawing_utils

# Function to map distance to servo angle
def map_distance_to_angle(distance, min_dist, max_dist, min_angle, max_angle):
    return int((distance - min_dist) * (max_angle - min_angle) / (max_dist - min_dist) + min_angle)

# OpenCV video capture
cap = cv2.VideoCapture(0)

while True:
    success, img = cap.read()
    if not success:
        break

    # Convert to RGB for MediaPipe
    img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    results = hands.process(img_rgb)

    if results.multi_hand_landmarks:
        for hand_landmarks in results.multi_hand_landmarks:
            # Get thumb tip and index finger tip coordinates
            thumb_tip = hand_landmarks.landmark[mp_hands.HandLandmark.THUMB_TIP]
            index_tip = hand_landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_TIP]

            h, w, _ = img.shape
            thumb_x, thumb_y = int(thumb_tip.x * w), int(thumb_tip.y * h)
            index_x, index_y = int(index_tip.x * w), int(index_tip.y * h)

            # Draw a line between the thumb and index finger
            cv2.line(img, (thumb_x, thumb_y), (index_x, index_y), (255, 0, 0), 3)
            cv2.circle(img, (thumb_x, thumb_y), 5, (0, 255, 0), cv2.FILLED)
            cv2.circle(img, (index_x, index_y), 5, (0, 255, 0), cv2.FILLED)

            # Calculate distance
            distance = math.hypot(index_x - thumb_x, index_y - thumb_y)

            # Map the distance to servo angle (0-90 degrees)
            angle = map_distance_to_angle(distance, min_dist=50, max_dist=200, min_angle=0, max_angle=180)
            angle = max(0, min(180, angle))  # Ensure angle is within bounds

            # Send angle to Arduino
            arduino.write(f"{angle}\n".encode())

            # Display distance and angle
            cv2.putText(img, f"Angle: {angle} deg", (10, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)

            # Draw landmarks
            mp_draw.draw_landmarks(img, hand_landmarks, mp_hands.HAND_CONNECTIONS)

    # Display the image
    cv2.imshow("Hand Tracking", img)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# Cleanup
cap.release()
cv2.destroyAllWindows()
arduino.close()

				
			
🚀 Testing the Project:
1️⃣ Run the Arduino code first.
2️⃣ Run the Python code and wait for the camera to start.
3️⃣ Move your thumb and index finger to change the servo angle! ⚙️✨
💬 I would love to hear your thoughts and feedback! Drop a comment or rate this project! 😊

اترك تعليقاً

لن يتم نشر عنوان بريدك الإلكتروني. الحقول الإلزامية مشار إليها بـ *

Previous